package ru.ifmo.tanya.service.impl;

import ru.ifmo.tanya.core.huntgroups.HuntGroup;
import ru.ifmo.tanya.core.users.User;
import ru.ifmo.tanya.db.HuntGroupManager;
import ru.ifmo.tanya.keepers.UserStatusKeeper;
import ru.ifmo.tanya.service.AuthorizedHttpService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

public class RemoveUserFromGroupService extends AuthorizedHttpService {

    private final UserStatusKeeper userStatusKeeper = new UserStatusKeeper();

    private final HuntGroupManager huntGroupManager = new HuntGroupManager();

    @Override
    public void invokeWithUser(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,String[]> params = request.getParameterMap();
        for (final String groupName : params.keySet()) {
            HuntGroup huntGroup = huntGroupManager.getHuntGroupByName(groupName);
            userStatusKeeper.removeGroupFromUser(user, huntGroup);
        }
        response.sendRedirect("/service/show/hunt/groups");
    }
}
